package io.zhile.research.intellij.ier.listener;

import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.application.ApplicationActivationListener;
import com.intellij.openapi.wm.IdeFrame;
import io.zhile.research.intellij.ier.common.Resetter;
import io.zhile.research.intellij.ier.helper.BrokenPlugins;
import io.zhile.research.intellij.ier.helper.Constants;
import io.zhile.research.intellij.ier.helper.NotificationHelper;
import io.zhile.research.intellij.ier.helper.ResetTimeHelper;
import org.jetbrains.annotations.NotNull;

public class AppActivationListener implements ApplicationActivationListener {

    /**
     * 标记 下面的逻辑是否处理过一次
     */
    private boolean tipped = false;

    public void applicationActivated(@NotNull IdeFrame ideFrame) {
        //清除损坏的插件列表文件
        BrokenPlugins.fix();

        // 试用判断试用是否超过25天, 没有超过就不走下面的逻辑
        if (tipped || !ResetTimeHelper.overResetPeriod()) {
            return;
        }

        tipped = true;
        String message = "你已经有太长时间没有重置试用时间了! \n 你要再次重置吗?";
        //根据用户是否设置自动重置, 来弹出包含不同的Action的通知
        if (Resetter.isAutoReset()) {
            AnAction  action = ActionManager.getInstance().getAction(Constants.RESTART_ACTION_ID);
            NotificationType  type = NotificationType.WARNING;
            NotificationHelper.show(null, null, null, message, type, action);
        }else{
            AnAction action = ActionManager.getInstance().getAction(Constants.RESET_ACTION_ID);
            NotificationType type = NotificationType.INFORMATION;
            NotificationHelper.show(null, null, null, message, type, action);
        }
    }

    /**
     * 失去焦点触发同样的逻辑
     *
     * @param ideFrame
     */
    @Override
    public void applicationDeactivated(@NotNull IdeFrame ideFrame) {
        applicationActivated(ideFrame);
    }

}
